package jniGlue;

import java.io.File;
import java.io.IOException;

import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
import javax.swing.JOptionPane;

import programm.OwnFileChooser;

/**
 * Class for linking to the dll-files which specifiy the connection to the
 * FX5-DMX Interface. Only for Windows (we tested with Win XP and Win 7)!
 * 
 * @author pd
 * 
 */

public class Windowsclass implements Librarylinker
{

	private String sPathArchive;
	private String sPath;
	private String usbdmxpath;

	public native void startdmx(String path);

	public native void senddmx(int channel, int value);

	public native void closedmx(String path);

	public boolean success = false;

	public Windowsclass(String sPathArchive)
	{
		this.sPathArchive = sPathArchive;
		sPath = sPathArchive + "src/WindowsSource/";

		try
		{
			System.load(sPath + "jniGlue_Windowsclass.dll");
			this.startdmx(sPath + "usbdmx.dll");
			success = true;
			// Everything went well!
		} catch (UnsatisfiedLinkError ue)
		{
			JOptionPane.showMessageDialog(new JDialog(),
					"jniGlue_Windowsclass.dll und usbdmx.dll konnten "
							+ "nicht gefunden werden!",
					"Bibliotheken nicht gefunden!", JOptionPane.ERROR_MESSAGE);

			// Create a file chooser
			sPath = (new OwnFileChooser(".dll",
					"Bitte jniGlue_Windowsclass.dll auswaehlen!").getPath());

			if (sPath == null)
			{
				// Then the CANCEL button was pressed
				success = false;
				JOptionPane.showMessageDialog(new JDialog(),
						"Abbruch! Programm wird beendet!", "Abbruch!",
						JOptionPane.ERROR_MESSAGE);
			} else
			{
				if (sPath.indexOf("jniGlue_Windowsclass.dll") != -1)
				{
					// load the Wrapperlibrary
					System.load(sPath);
					int t = sPath.indexOf("jniGlue_Windowsclass.dll");
					try
					{
						sPath = sPath.substring(0, t)+"usbdmx.dll";
						System.load(sPath);
					}
					catch(NullPointerException e)
					{
						sPath = (new OwnFileChooser(".dll",
							"Bitte usbdmx.dll auswaehlen!").getPath());
					}
					if (sPath == null)
					{
						// Then the CANCEL button was pressed
						success = false;
						JOptionPane.showMessageDialog(new JDialog(),
								"Abbruch! Programm wird beendet!", "Abbruch!",
								JOptionPane.ERROR_MESSAGE);
					} else
					{
						if (sPath.indexOf("usbdmx.dll") != -1)
						{
							// start the Interface with the path of the
							// usbdmx.dll
							usbdmxpath = sPath;
							this.startdmx(sPath);
							success = true;
							// Everything went well!
						} else
						{
							// Then a wrong lib was selected
							success = false;
							JOptionPane
									.showMessageDialog(
											new JDialog(),
											"Falsche Bibliothek ausgewaehlt! Programm wird beendet!",
											"Fehler!",
											JOptionPane.ERROR_MESSAGE);
						}
					}
				} else
				{
					// Then a wrong lib was selected
					success = false;
					JOptionPane
							.showMessageDialog(
									new JDialog(),
									"Falsche Bibliothek ausgewaehlt! Programm wird beendet!",
									"Fehler!", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}

	/**
	 * This Path is needed in the closedmx() function when the OS is Windows.
	 * When OS is Linux then an empty String is returned.
	 * 
	 * @return
	 */
	public String getUsbdmxPath()
	{
		return usbdmxpath;
	}

}
